﻿Imports System.Linq
Partial Class App_Connections
    Inherits MyUI.PageBaseApp

    Public CurrAccount As mDB.Account

    'Public WithEvents GroupConsoleSearch As UI.GroupButton(Of LinkButton)
    Public WithEvents GroupConsoleDay As UI.GroupButton(Of LinkButton)

    'Protected SortGridQuery, SortGridFollStaus As UI.SortControl

#Region "PAGE"

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        CurrAccount = CurrUser.CurrAccount

        'GroupConsoleSearch = New UI.GroupButton(Of LinkButton)(Me.ViewState, "CONSOLE_SEARCH", "bottone-private-a")
        GroupConsoleDay = New UI.GroupButton(Of LinkButton)(Me.ViewState, "CONSOLE_DAY", "pg-count-h")

        If Not Me.IsPostBack Then
            SetDataPage()
        End If

    End Sub

    Private Sub SetDataPage()

        'SetListAccount()

        If CurrAccount IsNot Nothing Then
            SetDataConsole()
            SetDataAccountFilter()
            SetDataAccountList()
        End If

    End Sub

#End Region

#Region "CONSOLE"

    Private Sub SetDataConsole()

        'UI.Controls.ClassSet(Me.RbtSearchLink, "active", Me.SearchType = 1)
        'UI.Controls.ClassSet(Me.RbtSearchCategory, "active", Me.SearchType = 2)
        'UI.Controls.ClassSet(Me.RbtSearchHashtag, "active", Me.SearchType = 3)


        Dim Cat = CurrAccount.
            Tab_Categorize_Follower.
            REL.Categorize_Follower.Categorize.
            SetSelect("Categorize.*").
        ToDataTable()
        UI.List.Bind(Me.DdlCategories, Cat, "description", "id", "", False)

        Dim link = MyApp.EnumToDic(Of mDB._Account_Link.Type)()
        UI.List.Bind(Me.DdlLinkType, link, "value", "key", "", False)

        'Dim orderby = MyApp.EnumToDic(Of OrderByType)()
        'UI.List.Bind(Me.DdlOrderBy, orderby, "value", "key", "0", False)

        UI.List.Bind(Me.DdlVerified, MyApp.EnumToDic(Of VerifiedType), "value", "key", "0", False)

        Dim gender = MyApp.EnumToDic(Of mDB.Gender_Name.Type)()
        gender.Remove(mDB.Gender_Name.Type.NOT_ASSIGNED)
        UI.List.Bind(Me.DdlGender, gender, "value", "key", "", False)

        Dim lang = DB.TAB.Language.ToList
        UI.List.Bind(Me.DdlLanguage, lang, "language", "lang_code", "", False)
        UI.List.Bind(Me.DdlLanguageNot, lang, "language", "lang_code", "", False)

        Dim business = MyApp.EnumToDic(Of mDB.Account.BusinessType)()
        business.Remove(mDB.Account.BusinessType.NOT_ASSIGNED)
        UI.List.Bind(Me.DdlBusiness, business, "value", "key", "", False)

        Dim Status = MyApp.EnumToDic(Of mDB.Account.StatusType)()
        Status.Remove(mDB.Account.StatusType.NOT_ASSIGNED)
        UI.List.Bind(Me.DdlStatus, Status, "value", "key", "", False)

        If Not Me.IsPostBack Then
            Me.GroupConsoleDay.SelectButton = Me.RbtDayAll
            Me.DdlLinkType.SelectedValue = mDB._Account_Link.Type.FOLLOWER
        End If



    End Sub
    Private Sub SetDataAccountFilter()

        Dim f = CurrAccount.Tab_Account_Filter.ToDataTable
        UI.List.Bind(Me.DdlAccountFilter, f, "description", "id", "", False)
        Me.DdlAccountFilter.Items.Insert(0, New ListItem("-- Select  --", "0"))

        UI.List.Bind(Me.DdlOnLineList, CurrAccount.Tab_List.ToDataTable, "fullname", "id", "", False)



    End Sub
    Protected Sub BtnAddList_Click(sender As Object, e As EventArgs) Handles BtnAddList.Click

        Dim accs = CurrAccount.View_AccountLink_ByFilter(Me.GetDataFilter).
            SetSelect("account.account_code").
            Limit(1000).
        ToList(Of String)("account_code")

        Dim tw As New MyApp.Network.Twitter(CurrAccount.GetOauth)
        tw.AddList(New mDB.List With {.Fullname = Me.TxtNewList.Text, .Account_Id = CurrAccount.Id}, accs)

    End Sub

    'Protected Sub GroupConsoleSearch_onselected(value As String) Handles GroupConsoleSearch.Selected
    '    SetDataAccountList()
    'End Sub


#End Region

#Region "FILTER"

    Protected Sub BtnAddFilter_Click(sender As Object, e As EventArgs) Handles BtnAddFilter.Click

        Dim f = New mDB.Account_Filter With {
            .Account_Id = CurrAccount.Id, .Description = Me.TxtNewFilter.Text
        }

        f.Filter = Me.GetDataFilter

        DB.InsertOnSubmit(f)
        DB.SubmitChanges()

        'f.SaveFilter_Data()

        SetDataAccountFilter()

    End Sub
    Protected Sub BtnRemoveFilter_Click(sender As Object, e As EventArgs) Handles BtnRemoveFilter.Click

        CurrAccount.Tab_Account_Filter.ByPK(Me.DdlAccountFilter.SelectedValue).Delete.ExecuteCommand()

        SetDataAccountFilter()
    End Sub
    Protected Sub BtnSaveFilter_Click(sender As Object, e As EventArgs) Handles BtnSaveFilter.Click

        Dim f = CurrAccount.Tab_Account_Filter.GetByPK(Me.DdlAccountFilter.SelectedValue)

        f.Filter = Me.GetDataFilter
        DB.SubmitChanges()

        SetDataAccountFilter()

    End Sub
    Protected Sub DdlAccountFilter_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DdlAccountFilter.SelectedIndexChanged
        Dim f = CurrAccount.Tab_Account_Filter.GetByPK(Me.DdlAccountFilter.SelectedValue)

        If f Is Nothing Then
            f = New mDB.Account_Filter
        End If

        SetDataFilter(f.Filter)

    End Sub
    Private Sub SetDataFilter(f As mDB.Account.Filter)

        Me.TxtUsername.Text = f.Username
        Me.TxtFullname.Text = f.Fullname
        Me.TxtBeginDate.Text = MyUI.Print.DateInput(f.FromDate)
        Me.TxtEndDate.Text = MyUI.Print.DateInput(f.ToDate)
        UI.List.SetSelectValues(Me.DdlLinkType, f.IdsLink)
        UI.List.SetSelectValues(Me.DdlBusiness, f.IdsBusin)
        UI.List.SetSelectValues(Me.DdlStatus, f.IdsStatus)
        UI.List.SetSelectValues(Me.DdlCategories, f.IdsCat)
        UI.List.SetSelectValues(Me.DdlGender, f.IdsGend)
        UI.List.SetSelectValues(Me.DdlLanguageNot, f.IdsLangNot)
        UI.List.SetSelectValues(Me.DdlLanguage, f.IdsLang)

    End Sub
    Private Function GetDataFilter() As mDB.Account.Filter

        Return New mDB.Account.Filter With {
            .FromDate = Utils.Conv.ToDate(Me.TxtBeginDate.Text), .ToDate = Utils.Conv.ToDate(Me.TxtEndDate.Text),
            .Username = Me.TxtUsername.Text, .Fullname = Me.TxtFullname.Text,
            .IdsLink = UI.List.GetSelectValuesInt(Me.DdlLinkType),
            .IdsBusin = UI.List.GetSelectValuesInt(Me.DdlBusiness),
            .IdsStatus = UI.List.GetSelectValuesInt(Me.DdlStatus),
            .IdsCat = UI.List.GetSelectValuesInt(Me.DdlCategories),
            .IdsGend = UI.List.GetSelectValuesInt(Me.DdlGender),
            .IdsLangNot = UI.List.GetSelectValues(Me.DdlLanguageNot),
            .IdsLang = UI.List.GetSelectValues(Me.DdlLanguage),
            .Verified = Me.DdlVerified.SelectedValue
        }
    End Function

#End Region

#Region "ACCOUNT LIST"
    Protected Enum OrderByType
        NONE = 0
        FOLLOWER
        RANK
    End Enum
    Protected Enum VerifiedType
        NONE = 0
        VER
        NOT_VER
    End Enum


    Protected Sub SetDataAccountList()

        Me.LblUserFound.Text = "-"

        Dim FromDate = Utils.Conv.ToDate(Me.TxtBeginDate.Text)
        Dim ToDate = Utils.Conv.ToDate(Me.TxtEndDate.Text)

        If FromDate = Date.MinValue Then
            FromDate = Now.AddDays(-30).Date
        End If
        If ToDate = Date.MinValue Then
            ToDate = Now.Date
        End If

        Me.TxtFullname.Enabled = True
        Me.TxtUsername.Enabled = True

        Me.PnlSearchLink.Visible = True
        Me.PnlAccountConn.Visible = True

        '-------------------------------------------------------
        ' QUERY
        '-------------------------------------------------------

        Dim q = CurrAccount.View_AccountLink_ByFilter(Me.GetDataFilter).
            Limit(1000)

        q.Select(" ((log(2,(if(follower_count=0,1,follower_count) / (power(if(friend_count=0,1,friend_count) , 0.1))) * ( power(if(list_memberships_count = 0,1,list_memberships_count) , 0.5 )) * (if(verified,1.3,1)))) - 6) / (30-6) *100 as rank ")
        Me.DPAccountConn.SortQuery(q)

        'Select Case DirectCast(Utils.Conv.ToInt(Me.DdlOrderBy.SelectedValue), OrderByType)
        '    Case OrderByType.NONE
        '    Case OrderByType.FOLLOWER
        '        q.SetOrderBy("follower_count desc")
        '    Case OrderByType.RANK
        '        'q.OrderBy("((follower_count /friend_count*(follower_count^1.1)) * (if(verified,1.3,1)) ) * (case status_type_id when 1 then 0.2 when 2 then 0.4 when 3 then 1.2 when 4 then 1.1 when 5 then 1 end ) desc")
        '        q.SetOrderBy("((follower_count /friend_count*(follower_count^1.1)) * (if(verified,1.3,1)) )  desc")

        'End Select


        Dim dt = q.ToDataTable()

        '-------------------------------------------------------
        ' LAYOUT
        '-------------------------------------------------------

        Dim DataBind As List(Of Object) = UI.ListData.Bind(Me.ListAccountConn, dt, {"id"})

        MyUI.Data.SetListStatusAccount(CurrAccount, UI.ListData.GetRowsList(Me.ListAccountConn), DataBind.Select(Function(o) o("account_code").ToString).ToList)
        MyUI.Data.SetListCatAccount(CurrAccount, UI.ListData.GetRowsList(Me.ListAccountConn), DataBind.Select(Function(o) CInt(o("id"))).ToList)

        If dt IsNot Nothing Then
            Me.LblUserFound.Text = IIf(dt.Rows.Count >= 1000, "999+", dt.Rows.Count)
        End If

        Me.UpPAccountConn.Update()


    End Sub
    'Protected Sub ListAccountConn_Sorting(sender As Object, e As ListViewSortEventArgs) Handles ListAccountConn.Sorting

    '    Me.dpdp.Sort(e.SortExpression)

    '    SetDataAccountList()

    'End Sub

    Public Function calc2(FollHours() As Integer) As Double

        'Dim FollHours = Me.GetActivityHours

        'calcolo indice cons
        Dim media = FollHours.Average
        Dim totdiff = 0
        For ct = 0 To FollHours.Length - 1
            'totdiff += Math.Abs(FollHours(ct) - FollHours(ct - 1))
            totdiff += Math.Abs(FollHours(ct) - media)
        Next
        Dim mediadiff = totdiff / (FollHours.Length)

        Return Utils.Math.Div(mediadiff, media)


    End Function
    Public Function calc3(FollHours() As Integer) As Double

        'Dim FollHours = Me.GetActivityHours

        'calcolo indice cons
        Dim tot = FollHours.Sum

        Dim Follord = FollHours.OrderByDescending(Function(o) o).ToList

        Dim mediadiff = Follord.Take(8).Sum

        Return Utils.Math.Div(mediadiff, tot)


    End Function

    'Public Function calc4(FollHours() As Integer) As Double

    '    'Dim FollHours = Me.GetActivityHours

    '    'calcolo indice cons
    '    Dim tot = FollHours.Sum

    '    Dim Follord = FollHours.OrderByDescending(Function(o) o).ToList
    '    Dim mediadiff = 0
    '    For ct = 0 To 7
    '        mediadiff += FollHours(ct)
    '    Next
    '    For ct = 20 To 23
    '        mediadiff += FollHours(ct)
    '    Next

    '    'Dim mediadiff = Follord.Skip( .Sum

    '    Return Utils.Math.Div(mediadiff, tot)


    'End Function
    Public Function calc4(FollHours() As Integer) As Double

        'Dim FollHours = Me.GetActivityHours

        'calcolo indice cons
        Dim tot = FollHours.Sum

        Dim Follord = FollHours.OrderByDescending(Function(o) o).ToList
        Dim mediadiff = 0
        For ct = 0 To 7
            mediadiff += FollHours(ct)
        Next
        For ct = 18 To 23
            mediadiff += FollHours(ct)
        Next

        'Dim mediadiff = Follord.Skip( .Sum

        Return Utils.Math.Div(mediadiff, tot)


    End Function

    Protected Function GetImageGenderType(b As mDB.Account.BusinessType, g As mDB.Gender_Name.Type) As String

        If b = GloukDB.Account.BusinessType.BUSINESS Then
            Return "business.png"
        Else
            If g = GloukDB.Gender_Name.Type.MALE Then
                Return "man.png"
            End If
            If g = GloukDB.Gender_Name.Type.FEMALE Then
                Return "woman.png"
            End If

        End If

        Return ""
    End Function
    Protected Function GetImageStatus(b As mDB.Account.StatusType) As String

        Select Case b
            Case GloukDB.Account.StatusType.FAKE
                Return "conn-fake.png"
            Case GloukDB.Account.StatusType.INACTIVE
                Return "conn-inactive.png"
            Case GloukDB.Account.StatusType.LOW_ACTIVITY, GloukDB.Account.StatusType.MEDIUM_ACTIVITY, GloukDB.Account.StatusType.HIGHT_ACTIVITY
                Return "conn-good.png"
            Case Else
                Return ""
        End Select

    End Function

    Protected Sub ChkExcluded_CheckedChanged(sender As Object, e As EventArgs)

        Dim chk As CheckBox = sender
        Dim row = UI.ListDataItem.GetRow(sender)
        Dim id = UI.ListDataItem.GetKey(row)

        'Dim link = CurrAccount.Tab_Account_Link(GloukDB.Account_Link.Type.FOLLOWER, id).ToObj
        Dim link = CurrAccount._Tab_Account_Link(mDB._Account_Link.Type.FOLLOWER, id).ToObj
        If link IsNot Nothing Then
            link.Excluded = chk.Checked

            DB.SubmitChanges()
        End If




    End Sub

    Protected Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click

        SetDataAccountList()

    End Sub


#End Region


End Class

'Public Class GroupButton_old

'    Public Property CacheState As Object
'    Public Property group As String
'    Public Property cssactive As String


'    Private Buttons As New List(Of Button)
'    Private p As Page

'    Public ReadOnly Property SelectValue() As String
'        Get
'            Return Me.SelectButton.CommandArgument
'        End Get
'    End Property

'    Public Property SelectButtonID() As String
'        Get
'            Return Me.CacheState("_GROUP_" & Me.group)
'        End Get
'        Set(ByVal value As String)
'            Me.CacheState("_GROUP_" & Me.group) = value
'        End Set
'    End Property

'    Public Property SelectButton() As Button
'        Get
'            Return (From b In Me.Buttons Where b.ID = Me.SelectButtonID).FirstOrDefault
'        End Get
'        Set(value As Button)
'            Me.SelectButtonID = value.ID

'            SetDataButtonList()

'        End Set
'    End Property

'    Public Event Selected(value As String)


'    Sub New(cache As Object, group As String, cssactive As String)


'        Me.p = HttpContext.Current.CurrentHandler
'        Me.CacheState = cache
'        Me.group = group
'        Me.cssactive = cssactive

'        Buttons = UI.Controls.FindCommandControls(Of Button)(Me.p.Controls, group)

'        For Each b In Buttons
'            AddHandler b.Click, AddressOf BntExport_Click
'        Next
'        SetDataButtonList()

'    End Sub

'    Private Sub SetDataButtonList()

'        For Each b In Buttons
'            UI.Controls.ClassSet(b, cssactive, b.ID = Me.SelectButtonID)
'        Next

'    End Sub


'    Protected Sub BntExport_Click(sender As Object, e As EventArgs)

'        Me.SelectButton = sender
'        RaiseEvent Selected(Me.SelectValue)

'    End Sub

'End Class

'Public Class GroupButton(Of tres As {IButtonControl, WebControl})

'    Public Property CacheState As Object
'    Public Property group As String
'    Public Property cssactive As String


'    Private Buttons As New List(Of tres)
'    Private p As Page

'    Public ReadOnly Property SelectValue() As String
'        Get
'            Return Me.SelectButton.CommandArgument
'        End Get
'    End Property

'    Public Property SelectButtonID() As String
'        Get
'            Return Me.CacheState("_GROUP_" & Me.group)
'        End Get
'        Set(ByVal value As String)
'            Me.CacheState("_GROUP_" & Me.group) = value
'        End Set
'    End Property

'    Public Property SelectButton() As tres
'        Get
'            Return (From b In Me.Buttons Where b.ID = Me.SelectButtonID).FirstOrDefault
'        End Get
'        Set(value As tres)
'            Me.SelectButtonID = value.ID

'            SetDataButtonList()

'        End Set
'    End Property

'    Public Event Selected(value As String)


'    Sub New(cache As Object, group As String, cssactive As String)


'        Me.p = HttpContext.Current.CurrentHandler
'        Me.CacheState = cache
'        Me.group = group
'        Me.cssactive = cssactive

'        Buttons = UI.Controls.FindCommandControls(Of tres)(Me.p.Controls, group)

'        For Each b In Buttons
'            AddHandler b.Click, AddressOf BntExport_Click
'        Next
'        SetDataButtonList()

'    End Sub

'    Private Sub SetDataButtonList()

'        For Each b In Buttons
'            UI.Controls.ClassSet(b, cssactive, b.ID = Me.SelectButtonID)
'        Next

'    End Sub


'    Protected Sub BntExport_Click(sender As Object, e As EventArgs)

'        Me.SelectButton = sender
'        RaiseEvent Selected(Me.SelectValue)

'    End Sub

'End Class







